package edu.xidian.bdilab.jrbigdata.service.bank.bankInfo.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import edu.xidian.bdilab.jrbigdata.controller.bank.BankCode;
import edu.xidian.bdilab.jrbigdata.controller.response.ResponseResult;
import edu.xidian.bdilab.jrbigdata.domain.BankInfo;
import edu.xidian.bdilab.jrbigdata.mapper.BankInfoMapper;
import edu.xidian.bdilab.jrbigdata.service.bank.bankInfo.BankInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static java.lang.Boolean.FALSE;
import static java.lang.Boolean.TRUE;

@Service
public class BankInfoServiceImpl implements BankInfoService {

    @Autowired
    BankInfoMapper bankInfoMapper;

    @Override
    public ResponseResult getAllBankInfo(Integer pageNum, Integer pageSize, String bankName,
                                         String region, String street, String description) throws Exception {
        QueryWrapper<BankInfo> bankInfoQueryWrapper = new QueryWrapper<>();
        PageHelper.startPage(pageNum, pageSize);
        BankInfo bankInfo;
        bankInfoQueryWrapper.like(bankName != null, "bank_name", bankName);
        bankInfoQueryWrapper.like(region != null, "regin", region);
        bankInfoQueryWrapper.like(street != null, "street", street);
        bankInfoQueryWrapper.like(description != null, "description", description);
        try {
            List<BankInfo> bankInfoList = bankInfoMapper.selectList(bankInfoQueryWrapper);
            PageInfo pageInfo = new PageInfo<>(bankInfoList);
            ResponseResult responseResult = new ResponseResult();
            Map<String, Object> data = new HashMap<>(3);
            data.put("governmentPolicies List", bankInfoList);
            data.put("Total Page Num", pageInfo.getPages());
            data.put("Total", pageInfo.getTotal());
            if (bankInfoList.size() == 0) {
                responseResult.setCode(BankCode.CHECK_NONE.getCode());
                responseResult.setMessage(BankCode.CHECK_NONE.getMessage());
                responseResult.setSuccess(TRUE);
            } else {
                responseResult.setData(data);
                responseResult.setCode(BankCode.CHECK_SUCCESS.getCode());
                responseResult.setMessage(BankCode.CHECK_SUCCESS.getMessage());
                responseResult.setSuccess(TRUE);
            }
            return responseResult;
        } catch (Exception e) {
            ResponseResult responseResult = new ResponseResult();
            responseResult.setCode(BankCode.CHENCK_FAILED.getCode());
            responseResult.setMessage(BankCode.CHENCK_FAILED.getMessage());
            responseResult.setSuccess(FALSE);
            return responseResult;
        }
    }
}
